<?php

namespace App\Admin\Controllers;

use App\ParkBaner;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use App\Park;
use Illuminate\Support\MessageBag;

class ParkBanerController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
       $park= Park::find(\Request::get('park_id'));
        return $content
            ->header($park->title ?? '')
            ->description('园区频道')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('园区频道')
            ->description('园区频道')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        $parkBaner=ParkBaner::find($id);
        $park=Park::find($parkBaner->park_id);
        return $content
            ->header($park->title)
            ->description('编辑园区栏目')
            ->body($this->form($parkBaner->park_id)->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        $park=Park::find(\Request::get('park_id'));
        return $content
            ->header($park->title)
            ->description('新建栏目')
            ->body($this->form($park->id));
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new ParkBaner);
        $park_id=\Request::get('park_id');
        $grid->model()->where('park_id',$park_id);
        $grid->id('Id');
        $grid->title('栏目标题');
        $grid->url('预览')->display(function($value){
            $url=config('admin.url').'/base/'.\Request::query('park_id').'/'.$this->id;
            return "<a href='$url' target='_blank'>预览</a>";
        });
        $grid->disableExport();
        $grid->disableFilter();

        $grid->disableCreateButton();
       $grid->tools(function (Grid\Tools $tools)use($park_id) {
            $tools->append('<div class="btn-group pull-right" style="margin-right: 10px">
    <a href="'.route('parkBaner.create',['park_id'=>$park_id]).'" class="btn btn-sm btn-success" title="新增">
        <i class="fa fa-save"></i><span class="hidden-xs">&nbsp;&nbsp;新增</span>
    </a>
</div>');
        });
        $grid->created_at('创建时间');
        $grid->updated_at('更新时间');

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(ParkBaner::findOrFail($id));

        $show->id('Id');
        $show->park_id('园区id');
        $show->title('栏目标题');
        $show->image('海报');
        $show->content('栏目内容');
        $show->created_at('创建时间');
        $show->updated_at('更新时间');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form($park_id=0)
    {
        $form = new Form(new ParkBaner);

        $form->hidden('park_id','园区')->default($park_id);
        $form->text('title', '栏目标题')->rules('required',[
            'required'=>'请输入栏目标题'
        ])->help('必填项');
        $form->image('image','海报')->help('选择则增加海报，不选择无');
        $form->ckeditor('content', '栏目内容');
        $form->tools(function (Form\Tools $tools) {

            // 去掉`列表`按钮
            $tools->disableList();

            // 去掉`删除`按钮
            $tools->disableDelete();

            // 去掉`查看`按钮
            $tools->disableView();

        });
        $form->saved(function($from){

            $success = new MessageBag([
                'title'   => '保存成功',
                'message' => '保存成功',
            ]);

            return redirect()->to('/admin/parkBaner?park_id='.$from->park_id)->with(compact('success'));
        });
        return $form;
    }
}
